// ------------------------------------------------------------------------
// |@Author       : Jarmin <edshop@qq.com>
// |@----------------------------------------------------------------------
// |@Date         : 2021-06-02 16:25:12
// |@----------------------------------------------------------------------
// |@LastEditTime : 2021-06-02 16:25:12
// |@----------------------------------------------------------------------
// |@LastEditors  : Jarmin <edshop@qq.com>
// |@----------------------------------------------------------------------
// |@Description  : Login.js For Admin
// |@----------------------------------------------------------------------
// |@FilePath     : login.js
// |@----------------------------------------------------------------------
// |@Copyright (c) 2021 http://www.ladmin.cn   All rights reserved. 
// ------------------------------------------------------------------------

$(function () {

    window.$body = $('body');

    /*! 后台加密登录处理 */
    $body.find('[data-login-form]').map(function (that) {
        (that = this), require(["md5"], function (md5) {
            $("form").vali(function (data) {
                var username = $.trim($(that).parent('#login').find('input[name="username"]').val());
                if (!username) return $.msg.error('登录账号不能为空!');
                if (username.length < 4) return $.msg.error('登录账号不能少于4位字符!');
                var password = $.trim($(that).parent('#login').find('input[name="password"]').val());
                if (!password) return $.msg.error('登录密码不能为空!');
                if (password.length < 4) return $.msg.error('登录密码不能少于4位字符!');
                var verify = $.trim($(that).parent('#login').find('input[name="verify"]').val());
                if (!verify) return $.msg.error('图形验证码不能为空!');
                data['password'] = md5.hash(md5.hash(data['password']) + data['uniqid']);
                $(that).parent('#login').addClass('checking');
                $(that).parent('#login').css('margin-left','-450px');
                $(that).parent('#login').parent().find('.auth').addClass('checking');
                setTimeout(function () {
                    $(that).parent('#login').css('margin-left','-180px');;
                    $(that).parent('#login').parent().find('.auth').removeClass('checking');
                    $(that).parent('#login').removeClass('checking');
                    $.form.load(location.href, data, "post", function (ret) {
                        if (parseInt(ret.code) !== 1) {
                            $(that).find('[data-captcha]').trigger('click');
                            $(that).find('.verify.info.layui-hide').removeClass('layui-hide');
                        }
                    }, null, null, 'false');
                }, 3000);
            });
        });
    });

    /*! 登录图形验证码刷新 */
    $body.on('click', '[data-captcha]', function () {
        var $that = $(this), $form = $that.parents('#login');
        var action = this.dataset.captcha || location.href;
        if (action.length < 5) return $.msg.tips('请设置验证码请求及验证地址');
        var type = this.dataset.captchaType || 'captcha-type', token = this.dataset.captchaToken || 'captcha-token';
        var uniqid = this.dataset.fieldUniqid || 'captcha-uniqid', verify = this.dataset.fieldVerify || 'captcha-verify';
        $.form.load(action, {type: type, token: token}, 'post', function (ret) {
            if (ret.code) {
                $that.html('<img alt="img" src="' + ret.data.image + '"><input type="hidden">').find('input').attr('name', uniqid).val(ret.data.uniqid || '');
                $form.find('[name="' + verify + '"]').attr('value', ret.data.code || '').val(ret.data.code || '');
                return (ret.data.code || $form.find('.verify.info.layui-hide').removeClass('layui-hide')), false;
            }
        }, false);
    });

    /*! 初始化登录图形 */
    $('[data-captcha]').map(function () {
        $(this).trigger('click');
    });
});